package com.quentinbeauperin.twitbird
{
	import com.coreyoneil.collision.CollisionList;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class Game extends Sprite
	{
		private var engine:Engine;
		private var rotatingMap:RotatingMap;
		private var screenEngine:ScreenEngine;
		
		public var bird:Bird;
		public var collisionList:CollisionList;
		public var ringGenerator1:RingGenerator;
		public var ringGenerator2:RingGenerator;
		public var obstacleGenerator1:ObstacleGenerator;
		public var obstacleGenerator2:ObstacleGenerator;
		public var textarea:TextField;
		public var boutonPause:Bouton;
		public var userInterface:UserInterface;
		public var score:int = new int();
		public var id:int = new int();
		
		public function Game(id:int)
		{
			super();
			this.id = id;
			trace("Coucou");
			addEventListener(Event.ADDED_TO_STAGE, build);
		}
		
		public function build(event:Event):void
		{
			// Create the moving background
			rotatingMap = new RotatingMap(id);
			addChild(rotatingMap);
			
			// Create the back layer
			ringGenerator1 = new RingGenerator(1);
			addChild(ringGenerator1);
			
			// Create the bird
			bird = new Bird(id);
			if (id == 5){
				bird.evil = true;
			}
			bird.x = 210;
			bird.y = 170;
			addChild(bird);
			
			// Create two obstacle generators
			obstacleGenerator1 = new ObstacleGenerator(1,id);
			addChild(obstacleGenerator1);
			obstacleGenerator2 = new ObstacleGenerator(2,id);
			addChild(obstacleGenerator2);
			
			// Create the front layer
			ringGenerator2 = new RingGenerator(2);
			addChild(ringGenerator2);
			
			// Create collision list
			collisionList = new CollisionList(bird);
			
			// Create informations
				// Score
			textarea = new TextField();
			textarea.text = "Score :" + score
			textarea.width = 100;
			textarea.height = 17;
			textarea.background = false;
			addChild(textarea);
			
				// Pause
			boutonPause = new Bouton(7);
			addChild(boutonPause);
			
			// Initialise the engine
			engine = new Engine();
			engine.level = id;
			engine.stage = stage;
			engine.bird = bird;
			engine.ringGenerator1 = ringGenerator1;
			engine.ringGenerator2 = ringGenerator2;
			engine.obstacleGenerator1 = obstacleGenerator1;
			engine.obstacleGenerator2 = obstacleGenerator2;
			engine.rotatingMap = rotatingMap;
			engine.buttonPause = boutonPause;
			engine.start();
			
			// Launch interface
			userInterface = new UserInterface();
			addChild(userInterface);
		}
	}
}